package sandbox;

// JaWE imports

// Common imports
import java.awt.*;
import java.io.*;
import java.util.*;

// SVG imports
import org.apache.batik.dom.*;
import org.apache.batik.svggen.*;
import org.w3c.dom.*;

// JPEG imports
import com.sun.image.codec.jpeg.*;
import org.enhydra.jawe.JaWEComponentSettings;
import org.enhydra.jawe.JaWEManager;
import org.enhydra.jawe.base.controller.JaWEController;
import org.enhydra.jawe.components.graph.Graph;
import org.enhydra.jawe.components.graph.GraphController;
import org.enhydra.shark.xpdl.elements.Package;
import org.enhydra.shark.xpdl.elements.WorkflowProcess;
import org.enhydra.shark.xpdl.elements.WorkflowProcesses;


/**
 * Class that uses JaWE to save the visual presentation of process definitions
 * defined in XPDL files as SVG and JPG pictures. The picture names will be
 * the same as the process definition Id, plus .svg/.jpg.
 * When calling main method of this class, user should pass the location of the
 * XPDL file he wants to process, i.e: java -cp <classpath> XPDLToSVGAndJPG test.xpdl
 */
public class ToSVG {

   public static void main (String[] args) {
      try {
        // importing XPDL into JaWE, and creating JaWE graphs;
        JaWEManager jm  = JaWEManager.getInstance();
        System.out.println(jm);
        JaWEController jc = jm.getJaWEController();
        System.out.println(jc);
         Package pkg = jc.openPackageFromFile("test.xml");
         //Package pkg=jawe.openDocument(args[0],true,false);S
         //PackageEditor pkgEd=jawe.getPackageEditor();
         //pkgEd.setNewPackage(pkg,true);
         WorkflowProcesses wps = pkg.getWorkflowProcesses();
         
          
         // Iterating through all process definitions, getting the graphical
         // presentation for each, and saving as SVG and JPG
         //WorkflowProcesses wps=(WorkflowProcesses)pkg.get("WorkflowProcesses");
         Iterator it = wps.toElements().iterator();
         //Iterator it=wps.toCollection().iterator();
         
         while (it.hasNext()) {
            WorkflowProcess wp =(WorkflowProcess)it.next();
            GraphController gc = new GraphController((JaWEComponentSettings)jc.getControllerSettings());
            gc.createGraph(wp);
            saveGraphAsSVG("test",gc.getSelectedGraph());
            
            //Process p=pkgEd.getProcessObject(wp);
            //AbstractEditor editor=p.getImplementationEditor();
            //AbstractGraph graph=editor.getGraph();

            // Resize (Otherwise not Visible)
            //graph.setSize(graph.getPreferredSize());

//            editor.getAction("SaveAsSVG").actionPerformed(null);
//            editor.getAction("SaveAsJPG").actionPerformed(null);
            //saveAsSVG(graph,wp.getID()+".svg");
            //saveAsJPG(graph,wp.getID()+".jpg");
         }
         System.exit(0);
      } catch (Exception ex) {
         ex.printStackTrace();
         System.exit(1);
      }
   }


   public static void saveGraphAsSVG(String file, Graph graph)
         throws Exception {
      FileOutputStream fos = new FileOutputStream(new File(file));
      // Created writer with UTF-8 encoding
      Writer out = new OutputStreamWriter(fos, JaWEManager.getInstance().getJaWEController().getControllerSettings().getEncoding());
      // Get a DOMImplementation
      DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
      // Create an instance of org.w3c.dom.Document
      Document document = domImpl.createDocument(null, "svg", null);
      // Create an instance of the SVG Generator
      SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
      // Render into the SVG Graphics2D implementation
      graph.paint(svgGenerator);
      // Use CSS style attribute
      boolean useCSS = true;
      // Finally, stream out SVG to the writer
      svgGenerator.stream(out, useCSS);
      // Close the file output stream
      fos.flush();
      fos.close();
   }

}
